package com.lagou.controller;

import com.lagou.dto.Page;
import com.lagou.pojo.TArticle;
import com.lagou.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    // http://localhost:8080/article/toIndexPage?since=0&limit=2
    @RequestMapping("/toIndexPage")
    public String toIndexPage(
            @RequestParam(value = "limit", required = true, defaultValue = "2") int limit,
            @RequestParam(value = "since", required = true, defaultValue = "0") int since,
            Model model) {
        List<TArticle> list = articleService.list(since, limit);
        int totalCount = articleService.totalCount();
        model.addAttribute("page", new Page(list, totalCount, since, limit));
        return "client/index";

    }
}
